package cn.ad.server.web.spring.service.httpclient.executor;

import cn.ad.server.web.log.IAdEventLogger;
import cn.ad.server.web.log.entity.AdTrafficHttpLog;
import cn.ad.server.web.spring.service.components.http.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * httpclient4执行。如果adEventLogger设置，则会记录http日志。
 * @author wangwei
 *
 */
public class HttpClientExtLogExecutor implements AdHttpExecutor {
	
	private static Logger LOGGER = LoggerFactory.getLogger(HttpClientExtLogExecutor.class);
	
	private AdHttpExecutor executor;
	
	
	public AdHttpExecutor getExecutor() {
		return executor;
	}

	public void setExecutor(AdHttpExecutor executor) {
		this.executor = executor;
	}
	/**
	 * Adserver 日志记录实现接口,如果不为null，记录；为null不记录。
	 */
	private IAdEventLogger adEventLogger;

	public IAdEventLogger getAdEventLogger() {
		return adEventLogger;
	}
	
	public void setAdEventLogger(IAdEventLogger adEventLogger) {
		this.adEventLogger = adEventLogger;
	}

	/**
	 * 请求后续执行
	 * @param <T>
	 * @param request
	 * @param response
	 */
	public <T> void doExecuteEnd(AdHttpGetRequest request,AdHttpResponse<T> response){
		logAdserverEventLog(request, response);
	}
	/**
	 * 请求后续执行
	 * @param <T>
	 * @param request
	 * @param response
	 */
	public <T> void doExecuteEnd(AdHttpPostRequest request,AdHttpResponse<T> response){
		logAdserverEventLog(request, response);
	}
	
	/**
	 * 记录广告事件日志
	 * @param request
	 * @param response
	 * @param time 请求响应时间（毫秒）。
	 */
	public void logAdserverEventLog(AdHttpGetRequest request,AdHttpResponse<?> response){
		if(adEventLogger!=null){			
			AdTrafficHttpLog log = new AdTrafficHttpLog();
			log.setHttpMethod("GET");
			pupulateLog(request, response,log);
			adEventLogger.log(log);
		}
		
	}
	
	/**
	 * 记录广告事件日志
	 * @param request
	 * @param response
	 * @param time 请求响应时间（毫秒）。
	 */
	public void logAdserverEventLog(AdHttpPostRequest request,AdHttpResponse<?> response){
		if(adEventLogger!=null){			
			AdTrafficHttpLog log = new AdTrafficHttpLog();
			log.setHttpMethod("POST");
			pupulateLog(request, response, log);
			adEventLogger.log(log);
		}
		
	}

	/**
	 * 填充log
	 * @param request
	 * @param response
	 * @param time
	 * @param log
	 */
	private void pupulateLog(AdHttpRequest request, AdHttpResponse<?> response, AdTrafficHttpLog log) {
		log.setRequstThreadName(Thread.currentThread().getName());
		log.setRequstTime(request.getRequstTime());
		log.setUrl(request.getUrl());
		log.setProtocol(response.getProtocol());
		log.setMajor(response.getMajor());
		log.setMinor(response.getMinor());
		log.setStatusCode(response.getStatusCode());
		log.setLength(response.getLength());
		log.setTime(response.getRespTime());
		LOGGER.debug(log.toString());
	}

	@Override
	public AdHttpResponse<?> execute(AdHttpGetRequest adHttpGetRequest) throws ResponseHandleException, IOException {
		AdHttpResponse<?> response = executor.execute(adHttpGetRequest);
		doExecuteEnd(adHttpGetRequest, response);
		return response;
	}

	@Override
	public AdHttpResponse<?> execute(AdHttpPostRequest adHttpPostRequest) throws ResponseHandleException, IOException {
		AdHttpResponse<?> response = executor.execute(adHttpPostRequest);
		doExecuteEnd(adHttpPostRequest, response);
		return response;
	}

	@Override
	public <T> AdHttpResponse<T> execute(AdHttpGetRequest adHttpGetRequest,
			AdResponseHandler<? extends T> responseHandler) throws ResponseHandleException, IOException {
		AdHttpResponse<T> response = executor.execute(adHttpGetRequest, responseHandler);
		doExecuteEnd(adHttpGetRequest, response);
		return response;
	}

	@Override
	public <T> AdHttpResponse<T> execute(AdHttpPostRequest adHttpPostRequest,
		AdResponseHandler<? extends T> responseHandler) throws ResponseHandleException, IOException {
		AdHttpResponse<T> response = executor.execute(adHttpPostRequest, responseHandler);
		doExecuteEnd(adHttpPostRequest, response);
		return response;
	}

	
	
}
